using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Xfinity.Engine;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace Xfinity.Console
{
    public class Console : DrawableGameComponent
    {
        #region Console trace listener
        public class ConsoleTraceListener : TraceListener
        {
            public override void Write(string message)
            {
                StandardConsole.Write(message);
            }

            public override void WriteLine(string message)
            {
                StandardConsole.WriteLine(message);
            }
        }
        #endregion

        public Console()
            : this(GameManager.Game)
        {

        }
        public Console(Game game)
            : base(game)
        {
            DrawOrder = int.MaxValue;
            Visible = false;
            game.Components.Add(this);
        }

        protected override void OnVisibleChanged(object sender, EventArgs args)
        {
            GraphicsHelper.DrawModels = !Visible;

            base.OnVisibleChanged(sender, args);
        }

        public override void Draw(GameTime gameTime)
        {
            if (StandardConsole.Transcript.Count <= 0 && StandardConsole.InputLine.Length == 0)
            {
                return;
            }

            int numberOfLinesFitting = (int)(GraphicsHelper.GraphicsDevice.Viewport.Height / GraphicsHelper.DefaultFont.LineSpacing);
            int linesToDraw;//= 0;// = numberOfLinesFitting - StandardConsole.Transcript.Count;

            if (StandardConsole.Transcript.Count < numberOfLinesFitting)
            {
                linesToDraw = StandardConsole.Transcript.Count;
            }
            else //if (StandardConsole.Transcript.Count >= numberOfLinesFitting)//make sure it fits!
            {
                linesToDraw = numberOfLinesFitting - 1;
            }


            int nextYPos = 0;
            //draw each of the visible transcript items
            for (int i = linesToDraw; i > 0; i--)
            {
                string text = StandardConsole.Transcript[StandardConsole.Transcript.Count - i];
                Color c;
                if (StandardConsole.IsFailMessage[StandardConsole.IsFailMessage.Count - i])
                {
                    c = Color.Red;
                }
                else
                {
                    c = Color.White;
                }
                GraphicsHelper.DrawText(text, new Vector2(10, nextYPos), c);
                nextYPos += GraphicsHelper.DefaultFont.LineSpacing;
            }

            DrawInputLine(nextYPos);


            base.Draw(gameTime);
        }

        private static void DrawInputLine(int nextYPos)
        {
            //draw input line
            string input = StandardConsole.InputLine.ToString();
            Vector2 size = GraphicsHelper.DefaultFont.MeasureString(input);
            Vector2 origin;
            if (size.X > GraphicsHelper.GraphicsDevice.Viewport.Width - 10)
            {
                origin = new Vector2(GraphicsHelper.GraphicsDevice.Viewport.Width - size.X, nextYPos);
            }
            else
            {
                origin = new Vector2(10, nextYPos);
            }
            GraphicsHelper.DrawText(input, origin, Color.White);
        }

    }
}
